package swordfish.test;

import imp.avos.BAVActivity;
import imp.avos.BAVObject;
import imp.avos.ERequestType;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import base.speeader.InjectView;
import base.speeader.Injector;
import base.utils.Logger;

import com.swordfish.lib.R;

/**
 * This is test activity for MVC
 * 
 * @author Fish
 * 
 */
public class MainActivity extends BAVActivity implements OnClickListener {

//	@InjectView(R.id.des)
	private TextView mDes;

//	@InjectView(R.id.progress_bar)
	private ProgressBar mProgressBar;

	private PersonAV mPerson;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_act);
		Injector.get(this, this).inject();

		mPerson = new PersonAV(AV_TABLE.HELLO_AV);

	}

	void printInfoClear(String info) {
		print(info, false);
	}

	void printInfoAppend(String info) {
		print(info, true);
	}

	void print(String info, boolean isAppend) {
		if (isAppend) {
			mDes.setText(mDes.getText() + "\n" + info);
		} else {
			mDes.setText(info);
		}
		Logger.verbose(TAG, info);
	}

	@Override
	public void onFinish(BAVObject request, BAVObject result) {
		// TODO Auto-generated method stub
		super.onFinish(request, result);
		mProgressBar.setVisibility(View.INVISIBLE);
		if (request instanceof PersonAV) {
			PersonAV data = (PersonAV) result;
			switch (request.getRequestType()) {
			case SAVE:
				printInfoClear("Save success !");
				break;

			case GET_LIST:
				printInfoClear("Search complete");
				List<PersonAV> list = data.getList();
				printInfoAppend("\t list.size() -> " + list.size());
				for (PersonAV o : list) {
					printInfoAppend(o.toString());
				}
				break;

			case DELETE:
				printInfoClear("Delete success !");
				break;

			default:
				break;
			}
		}
	}

	@Override
	public void onError(BAVObject request, Exception e) {
		// TODO Auto-generated method stub
		super.onError(request, e);
		// Logger.verbose(TAG, "\t " + e.getMessage());
		e.printStackTrace();
		mProgressBar.setVisibility(View.INVISIBLE);

	}

	@Override
	public void onProgress(BAVObject request, int progress) {
		// TODO Auto-generated method stub
		super.onProgress(request, progress);
		mProgressBar.setVisibility(View.VISIBLE);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
//		case R.id.delete:
//			mPerson.setRequestType(ERequestType.DELETE);
//			getModel().doRequestActually(mPerson);
//			break;
//		case R.id.save:
//			mPerson.setRequestType(ERequestType.SAVE);
//			getModel().doRequestActually(mPerson);
//			break;
//		case R.id.search:
//			mPerson.setToRefresh();
//			mPerson.setRequestType(ERequestType.GET_LIST);
//			getModel().doRequestActually(mPerson);
//			break;
//
//		default:
//			break;
		}
	}

}
